home *** CD-ROM | disk | FTP | other *** search
/ Nautilus 1992 July / Nautilus-3-8 / Nautilus-3-8.bin / Tools & Utilities / Techy Stuff / Source ƒ / ASM 2.0 ƒ / srec.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-15  |  1.4 KB  |  90 lines

  1. #include <stdio.h>
  2. #include <ctype.h>
  3.  
  4. FILE    *Sfile = NULL;
  5. int     checksum = 0;
  6.  
  7. #define S1MAX   0xFFFF
  8. #define S2MAX   0xFFFFFF
  9.  
  10. srecinit(s)
  11. char *s;
  12. {
  13.     if( (Sfile = fopen(s,"w")) == NULL )
  14.         fatal("Can't open output file");
  15.     srec('0',strlen(s),0,s);
  16. }
  17.  
  18. srecdata(count,addr,buf)
  19. int count;
  20. unsigned addr;
  21. char *buf;
  22. {
  23.     char    type;
  24.  
  25.     if( addr <= S1MAX )
  26.         type = '1';
  27.     else if( addr <= S2MAX )
  28.         type = '2';
  29.     else
  30.         type = '3';
  31.     srec(type,count,addr,buf);
  32. }
  33.  
  34. srecend(addr)
  35. int addr;
  36. {
  37.     char    type;
  38.  
  39.     if( addr <= S1MAX )
  40.         type = '9';
  41.     else if(addr <=S2MAX)
  42.         type = '8';
  43.     else
  44.         type = '7';
  45.     srec(type,0,addr,&type);
  46.     fclose(Sfile);
  47. }
  48.  
  49. srec(type,count,addr,buf)
  50. char type;
  51. int count;      /* number of data bytes */
  52. unsigned addr;
  53. char *buf;      /* data bytes */
  54. {
  55.     static int alen[10] = { 2, 2, 3, 4, 0, 0, 0, 4, 3, 2 };
  56.  
  57.     fprintf(Sfile,"S%c",type);    /* record header */
  58.     checksum = 0;
  59.     put2hex(count+alen[type-'0']+1);
  60.     switch(alen[type-'0']){
  61.     case 4:
  62.         put2hex(addr>>24);
  63.     case 3:
  64.         put2hex(addr>>16);
  65.     case 2:
  66.         put2hex(addr>>8);
  67.         put2hex(addr);
  68.         }
  69.     while(count--)
  70.         put2hex(*buf++);
  71.  
  72.     put2hex(~checksum);
  73.     fprintf(Sfile,"\n");
  74. }
  75.  
  76. /*
  77.  *      put2hex --- print low byte of integer in hex
  78.  *
  79.  *      Also, update checksum
  80.  */
  81. put2hex(b)
  82. int b;
  83. {
  84.     static char hexstr[] = "0123456789ABCDEF";
  85.  
  86.     b &= 0xFF;
  87.     fprintf(Sfile,"%c%c",hexstr[(b>>4)&0xF],hexstr[b&0xF]);
  88.     checksum += b;
  89. }
  90.